package service

import (
	"database-platform/dal/db"
	"database-platform/logger"
	"database-platform/model"
	"fmt"
)

// GetClusterList 获取集群列表
func GetClusterList(params *model.ClusterListRequest) (*model.ClusterListResponse, error) {
	var response = &model.ClusterListResponse{}

	var request = &model.Cluster{
		Name:        params.Name,
		DBType:      params.DBType,
		BusinessOne: params.BusinessOne,
		BusinessTwo: params.BusinessTwo,
		Level:       params.Level,
		ClusterType: params.ClusterType,
		Env:         params.Env,
		Unit:        params.Unit,
		Status:      params.Status,
	}

	result, count, err := db.ListClusterRecords(request, params.Page, params.PageSize, params.Search)
	if err != nil {
		return nil, err
	}
	msg := fmt.Sprintf("[service.GetClusterList], result: %v, count:%d, request: %v", result, count, request)
	logger.Logger.Warn(msg)

	response.Count = count
	response.Records = result
	response.Page = params.Page
	response.PageSize = params.PageSize

	return response, nil
}

func GetClusterById(id int) (*model.Cluster, error) {
	result, err := db.GetClusterRecordById(id)
	if err != nil {
		return nil, err
	}
	msg := fmt.Sprintf("[service.GetClusterById], result: %v,  id: %v", result, id)
	logger.Logger.Warn(msg)

	return result, nil
}

// InsertCluster 插入集群记录
func InsertCluster(cluster *model.Cluster) error {
	err := db.InsertClusterRecord(cluster)
	if err != nil {
		return err
	}

	msg := fmt.Sprintf("[service.InsertCluster],  cluster: %v", cluster)
	logger.Logger.Warn(msg)

	return nil
}

// UpdateCluster 更新集群记录
func UpdateCluster(id int, cluster *model.Cluster) error {
	err := db.UpdateClusterRecord(id, cluster)
	if err != nil {
		return err
	}

	msg := fmt.Sprintf("[service.UpdateCluster],  cluster: %v", cluster)
	logger.Logger.Warn(msg)

	return nil
}

// DeleteCluster 删除集群记录
func DeleteCluster(id int) error {
	err := db.DeleteClusterRecord(id)
	if err != nil {
		return err
	}

	msg := fmt.Sprintf("[service.DeleteCluster],  id: %v", id)
	logger.Logger.Warn(msg)

	return nil
}
